package com.lz.football.module.playerCenter
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.constants.TipsConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.PlayerCenterEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.lz.football.module.club.item.BaseItem;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.base.GoodsUsePanel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.managers.TipsManager;
	
	import mx.effects.Move;

	/**
	 * 球员listItem 
	 * @author Administrator
	 * 
	 */	
	public class ListItem extends BaseItem
	{
		
//		/**
//		 *序列 
//		 */
//		private var order:TextField;
		
		/**
		 *球员名称 
		 */
		private var playerName:TextField;
		/**
		 *位置 
		 */
		private var place:TextField;
		
		
		
		
		/**
		 *球员颜色 
		 */
		private var playercolor:MovieClip;
		/**
		 * 球星 
		 */
		private var _colorBitmap:Bitmap;
		
		/**
		 *等级 
		 */
		private var Lv:TextField;
		/**
		 *号码 
		 */
		private var playNumber:TextField;

		/**
		 *综合能力 
		 */
		private var ability:TextField;
		/**
		 *体力进度条 
		 */
		private var progressbar:MovieClip;
		/**
		 *体力 
		 */
		private var power:TextField;
		/**
		 *增加体力按钮 
		 */
		private var powerBtn:SimpleButton;
		/**
		 *状态按钮 
		 */
		private var buffBtn:SimpleButton;
		/**
		 *工资  
		 */
		private var salary:TextField;
		/**
		 *旗帜容器 
		 */
		private var bannerContain:MovieClip;
		/**
		 *列表背景 
		 */
		private var listBg:MovieClip;
		/**
		 * 
		 */
		private var _playerVo:PlayerVo;
		
		/**
		 *锁 
		 */
		private var lock:MovieClip;
		/**
		 *开格子说明 
		 */
		private var openDes:MovieClip;
		/**
		 *体力条 
		 */
		private var physicalBar:MovieClip;
		private var _isShowTips:Boolean;
		
		
		private var _playerStartVo:PlayerStarVo;
		/**
		 * 
		 */
		private var _data:Object;
		/**
		 *锁 
		 */
		private var lockIcon:MovieClip;
		public function ListItem()
		{
			super();
		}
		/**
		 * 
		 * @param view
		 * 
		 */
		override public function initUI(view:Object=null):void
		{
			_skin = Reflection.createInstance("com.liangzhong.ui.playerLittleCard");
			this.addChild(_skin);
			_skin.gotoAndStop(1);
			playerName = _skin.playerName;
			place = _skin.place;
			physicalBar = _skin.physicalBar;
			lockIcon = _skin.lockIcon;
			reset();
//			physicalBar.visible = false;
//			physicalBar.parent.removeChild(physicalBar);
//			contain = _skin.contain;
//			_skin.removeChild(physicalBar);
			super.initUI(_skin);
			return;
			progressbar = _skin.progressbar.progressbar;
			_skin.progressbar.mouseChildren = false;
			_skin.progressbar.mouseEnabled = false;
//			order = _skin.order;
			playercolor = _skin.playercolor;
			
			Lv = _skin.Lv;
			playNumber = _skin.playNumber;
			
			ability = _skin.ability;
			powerBtn = _skin.powerBtn;
			buffBtn = _skin.buffBtn;
			salary = _skin.salary;
			bannerContain = _skin.bannerContain;
			power = _skin.power;
			listBg = _skin.listBg;
			lock = _skin.lock;
			openDes = _skin.openDes;
			lock.visible = false;
			openDes.visible = false;
			super.initUI();
			progressbar.mouseChildren = false;
			progressbar.mouseEnabled = false;
//			order.mouseEnabled = false;
			playercolor.mouseChildren = false;
			playercolor.mouseEnabled = false;
			playerName.mouseEnabled = false;
			Lv.mouseEnabled = false;
			playNumber.mouseEnabled = false;
			place.mouseEnabled = false;
			ability.mouseEnabled = false;
			
			salary.mouseEnabled = false;
			bannerContain.mouseChildren = false;
			bannerContain.mouseEnabled = false;
			power.mouseEnabled = false;
			lock.mouseChildren = false;
			lock.mouseEnabled =false;
			openDes.mouseEnabled = false;
			openDes.mouseEnabled = false;
			DisplayMouseEventManager.instance.addTipsDisplay(powerBtn,"恢复体力");
			DisplayMouseEventManager.instance.addTipsDisplay(buffBtn,"球员受伤，能力下降");
		}
		/**
		 *显示数据 
		 * 
		 */
		public function showInfo():void
		{
//			colorChange();
//			updataValue();
//			
			_skin.gotoAndStop(_playerVo.color);
			place.text = _playerVo.place_String;
			if(_playerVo.starVo == null)return;
			playerName.text = _playerVo.starVo.pname + "";
//			setImageSorce(_playerVo.starVo.bitmapData);
			DisplayObjectUtil.removeAllChildren(contain);
			var bit:Bitmap = new Bitmap(_playerVo.starVo.bitmapData);
			contain.addChild(bit);
			physicalBar.bar.width = _playerVo.physical / (_playerVo.maxphysical <=0? 1:_playerVo.maxphysical) * 43.25;
			//国家
		}
		/**
		 *球星信息 
		 * 
		 */		
		private function showStarInfo():void
		{
			_skin.gotoAndStop(_playerStartVo.color);
			place.text =  ClubConst.turnPlace(_playerStartVo.place);
			playerName.text = _playerStartVo.pname;
			//			setImageSorce(_playerVo.starVo.bitmapData);
			DisplayObjectUtil.removeAllChildren(contain);
			var bit:Bitmap = new Bitmap(_playerStartVo.bitmapData);
			contain.addChild(bit);
			
			
		}
		/**
		 *移除_skin.progressbar 
		 * 
		 */
		public function removeBar():void
		{
//			if(physicalBar.parent)
//				physicalBar.parent.removeChild(physicalBar);
			physicalBar.visible = false;
		}
		/**
		 *添加  skin.progressbar 
		 * 
		 */
		public function addBar():void
		{
//			_skin.addChild(physicalBar);
			physicalBar.visible = true;
		}
		
		/**
		 * 设置锁状态 
		 * @param value
		 * @return 
		 * 
		 */
		public function set lockIconVisible(value:Boolean):void
		{
			lockIcon.visible = value;
		}
		override public function get width():Number
		{
			return 52;
		}
		override public function get height():Number
		{
			if(physicalBar.visible == true)return 74;
				
			return 67;
		}
		
		
		/**
		 *重置 
		 * 
		 */
		override public function reset():void
		{
			removeBar();
			_skin.gotoAndStop(1);
			playerName.text = "";
			place.text = "";
			DisplayObjectUtil.removeAllChildren(contain);
			callBack = null;
			_playerVo = null;
			removeEvtListener();
			lockIcon.visible = false;
			select = false;
			return;
			
			if(_colorBitmap != null)_colorBitmap.bitmapData = null;
			
			Lv.text = "";
			playNumber.text = "";
			
			ability.text = "";
			salary.text = "";
			power.text = "";
			_skin.progressbar.visible = false;
			powerBtn.visible = false;
			buffBtn.visible = false;
			lock.visible = false;
			openDes.visible = false;
			callBack = null;
			_playerVo = null;
		}
		/**
		 *增加球员位 
		 * 
		 */
		public function addPlayerNumber():void
		{
			reset();
//			lock.visible = true
//			openDes.visible = true;
//			order.text= "";
//			this.addEventListener(MouseEvent.CLICK,onClickHandler);
//			listBg.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		/**
		 *球员颜色改变 
		 * 
		 */
		public function colorChange():void
		{
			if(_colorBitmap == null)_colorBitmap = new Bitmap();
			_colorBitmap.bitmapData = null;
			_colorBitmap.bitmapData = Reflection.createInstance("com.lz.modul.playerColor_" + _playerVo.color) as BitmapData
			playercolor.addChild(_colorBitmap);
		}
		
		/**
		 *更新数值  
		 * 
		 */
		public function updataValue():void
		{
			if(_playerVo == null)return;
			physicalBar.bar.width = _playerVo.physical / (_playerVo.maxphysical <=0? 1:_playerVo.maxphysical) * 43.25;
			return;
			
			
			_skin.progressbar.visible = true;
			powerBtn.visible = true;
			buffBtn.visible = true;
			if(_playerVo.starVo != null)
				playerName.text = _playerVo.starVo.pname;
			Lv.text = _playerVo.level + "";
			playNumber.text = _playerVo.number+"";
			place.text = _playerVo.place_String + "";
			ability.text = _playerVo.ability + "";
			salary.text = _playerVo.salary + "";
			power.text = _playerVo.physical + "/" + _playerVo.maxphysical;
			progressbar.width = (_playerVo.physical / _playerVo.maxphysical) *77// _skin.progressbar.width;
			if(_playerVo.status ==1)
			{
				buffBtn.visible = false;
			}else
			{
				buffBtn.visible = true;
			}
			if(_playerVo.physical == _playerVo.maxphysical)
			{
				powerBtn.visible = false;
			}
			else
			{
				powerBtn.visible = true;
			}
//			DisplayObjectUtil.removeAllChildren(bannerContain);//国家图标
//			var bimap:Bitmap = new Bitmap();
		}
		/**
		 *更新物品使用面板 
		 * type:1为状态 2为体力
		 */
		public function updataGoodsPanel($obj:Object):void
		{
//			if(!GoodsUsePanel.inistance.parent)return;
//			GoodsUsePanel.inistance.initPlace();
//			if($obj.type == 1)
//			{
//				GoodsUsePanel.inistance.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.MEDICALCARD);
//				GoodsUsePanel.inistance.callBack = useBuffGoods;
//			}else if($obj.type == 2)
//			{
//				GoodsUsePanel.inistance.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.PHYSICAL);
//				GoodsUsePanel.inistance.callBack = usePowerGoods;
//			}
		}
		protected function onClickHandler(event:MouseEvent):void
		{
			if(_select)return;
			if(callBack != null)callBack.apply(null,[this]);
		}
		/**
		 * 
		 * 
		 */
		public function setBg($type:int):void
		{
			if($type % 2 !=0)
				listBg.alpha = 0;
			else
				listBg.alpha = 1;
//			order.text =/* ($type+1) + */"";
		}
		override public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.ROLL_OUT,onMouseRollOut);
			this.addEventListener(MouseEvent.ROLL_OVER,onMouseRollOver);
//			powerBtn.addEventListener(MouseEvent.CLICK,onPowerHandler);
//			buffBtn.addEventListener(MouseEvent.CLICK,onBuffHandler);
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		override public function onMouseRollOut(event:MouseEvent):void
		{
			super.onMouseRollOut(event);
//			TipsManager.hide();
//			TipsManager.hide();
		}
		override public function onMouseRollOver(event:MouseEvent):void
		{
			super.onMouseRollOver(event);
//			if(data == null)return;
//			if(_select)return;
//			if(frame != null)
//				frame.visible = true;
			if(!_isShowTips)return;
			if(data is PlayerVo)
			{
				TipsManager.show(TipsConst.PLAYERCARDTIPSBYPLAYER,{data:data,type:2},20,10,true);
			}else if(data is PlayerStarVo)
			{
				TipsManager.show(TipsConst.PLAYERCARDTIPS,data,15,15,false);
			}
			
			
		}
		/**
		 * 改变状态 
		 * @param event
		 * 
		 */
		protected function onBuffHandler(event:MouseEvent):void
		{
			if(_playerVo.status ==1)
			{
				Alert.showErrorTip("球员状态良好，不需要回复");
				return;
			}
			GoodsUsePanel.inistance.initPlace();
//			GoodsUsePanel.inistance.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.MEDICALCARD);
			GoodsUsePanel.inistance.setShowGoodsType(GoodsType.OTHER,OtherGoodType.MEDICALCARD);
			GoodsUsePanel.inistance.callBack = useBuffGoods;
			
		}
		/**
		 *使用buff物品 
		 * @param $goodsVo
		 * 
		 */
		private function useBuffGoods($goodsVo:GoodsVo):void
		{
			if($goodsVo == null)
			{
				Alert.show("治疗该受伤球员需要花费25000欧元，确定吗？","",Alert.NO,GlobalDef.AlertContainer,useBuff);
				function useBuff($type:int):void
				{
					if($type == Alert.YES)
					{
						AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REPLY_BUFF,{type:1,id:(data as PlayerVo).playerid,cellId:0}));
					}
				}
				return;
			}
			var cellid:int ;
			
			cellid = $goodsVo.pid;
			AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REPLY_BUFF,{type:1,id:(data as PlayerVo).playerid,cellId:cellid}));
		}
		/**
		 *增加体力 
		 * @param event
		 * 
		 */
		protected function onPowerHandler(event:MouseEvent):void
		{
			if(_playerVo.physical == _playerVo.maxphysical)
			{
				Alert.showErrorTip("体力充沛，不需要回复");
				return;
			}
			GoodsUsePanel.inistance.initPlace();
//			GoodsUsePanel.inistance.goodsList = AppData.getInstance().bagDataManager.getGoodsListByTwoTypes(GoodsType.OTHER,OtherGoodType.PHYSICAL);
			
			GoodsUsePanel.inistance.setShowGoodsType(GoodsType.OTHER,OtherGoodType.PHYSICAL);
			GoodsUsePanel.inistance.callBack = usePowerGoods;
			
			
		}
		/**
		 *使用体力物品增加体力 
		 * @param $goodsVo
		 * 
		 */
		private function usePowerGoods($goodsVo:GoodsVo):void
		{
			if($goodsVo == null)
			{
				Alert.show("治疗该受伤球员需要花费25000欧元，确定吗？","",Alert.NO,GlobalDef.AlertContainer,useBuff);
				function useBuff($type:int):void
				{
					if($type == Alert.YES)
					{
						AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REPLY_PLAYER_POWER,
							{type:1,id:(data as PlayerVo).playerid,cellId:0}));
					}
				}
				return;
			}
			var cellid:int ;
			cellid = $goodsVo.pid;
			AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.REPLY_PLAYER_POWER,
				{type:1,id:(data as PlayerVo).playerid,cellId:cellid}));
		}
		override public function set data(value:Object):void
		{
			_data = value;
			if(value is PlayerVo)
			{
				_playerVo = value as PlayerVo;
				showInfo();
			}else
			{
				_playerStartVo = value as PlayerStarVo;
				showStarInfo();
			}
			
			addEvtListener();
		}
		/**
		 * 获取data内容 
		 * @return 
		 * 
		 */
		override public function get data():Object
		{
			return _data;
		}
		/**
		 * 
		 * 
		 */
		override public function removeEvtListener():void
		{
			this.removeEventListener(MouseEvent.ROLL_OUT,onMouseRollOut);
			this.removeEventListener(MouseEvent.ROLL_OVER,onMouseRollOver);
			super.removeEvtListener();
			return;
			this.removeEventListener(MouseEvent.CLICK,onClickHandler);
			
			
			powerBtn.removeEventListener(MouseEvent.CLICK,onPowerHandler);
			buffBtn.removeEventListener(MouseEvent.CLICK,onBuffHandler);
			listBg.removeEventListener(MouseEvent.CLICK,onClickHandler);
		}
		override public function destroyView():void
		{
			removeEvtListener();
			progressbar = null;
			_playerVo = null;
			
			super.destroyView();
		}

		/**
		 *是否显示tips 
		 */
		public function get isShowTips():Boolean
		{
			return _isShowTips;
		}

		/**
		 * @private
		 */
		public function set isShowTips(value:Boolean):void
		{
			_isShowTips = value;
		}

	}
}